﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickerClimber.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ClickerClimber.Models
{
    public class Jimbo :IGameObject, IGameObjectDrawable
    {
        private enum JimboState
        {
            IDLE,
            MOTION,
            BUILDING
        }

        private JimboState _state;
        private Texture2D _jimboTexture;
        private Texture2D _jimboMotionTexture;
        private Texture2D _line;

        private Vector2 _position;
        private Vector2 _lastPosition;

        private float _timer;

        public void Initialize()
        {
            _state = JimboState.IDLE;
            _position = new Vector2(0, 0);
            _lastPosition = new Vector2(0, 0);
        }

        public void Update(GameTime gameTime)
        {
            switch (_state)
            {
                case JimboState.IDLE:
                    if (_lastPosition != _position)
                    {
                        _timer = GameParameters.JIMBO_MOTION_TIME;
                        AudioManager.Instance.PlaySound(AudioManager.eSound.blockCreated);
                        _state = JimboState.MOTION;
                    }
                    break;
                case JimboState.MOTION:
                    _timer -= gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
                    if (_timer <= 0.0)
                    {
                        _lastPosition = _position;
                        _state = JimboState.IDLE;
                    }
                    break;
                case JimboState.BUILDING:
                    break;
                default:
                    break;
            }
        }

        public void LoadTextures(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            _jimboTexture = content.Load<Texture2D>(GameParameters.JIMBO_TEXTURE_NAME);
            _jimboMotionTexture = content.Load<Texture2D>(GameParameters.JIMBO_MOTION_TEXTURE_NAME);
        }

        public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch batch)
        {
            Vector2 newPosition;

            if (_line == null)
            {
                _line = new Texture2D(batch.GraphicsDevice, 1, 1);
                _line.SetData(new Color[] { Color.Green });
            }

            switch (_state)
            {
                case JimboState.IDLE:
                    newPosition = _position + GameParameters.SCROLL_OFFSET;
                    newPosition.Y -= _jimboTexture.Height;
                    newPosition.X -= _jimboTexture.Width / 2;
                    batch.Draw(_jimboTexture, newPosition, Color.White);
                    //batch.Draw(_line, new Rectangle((int)newPosition.X, (int)newPosition.Y+2*_jimboTexture.Height/3, 50, 1), Color.Green);
                    break;
                case JimboState.MOTION:
                    newPosition = _lastPosition + GameParameters.SCROLL_OFFSET;
                    newPosition.Y -= _jimboMotionTexture.Height;
                    newPosition.X -= _jimboMotionTexture.Width / 2;
                    batch.Draw(_jimboMotionTexture, newPosition, Color.White);
                    //batch.Draw(_line, new Rectangle((int)newPosition.X, (int)newPosition.Y+2*_jimboMotionTexture.Height/3, 50, 1), Color.Green);
                    break;
                case JimboState.BUILDING:
                    break;
                default:
                    break;
            }
        }

        #region Properties

        public Vector2 Position
        {
            set
            {
                _position = value;
            }
            get
            {
                return _position;
            }
        }

        public int Height
        {
            get
            {
                return _jimboTexture.Height;
            }
        }

        #endregion Properties
    }
}
